package org.lq.shopping.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.lq.shopping.dao.ShopManagerDao;
import org.lq.shopping.dao.impl.ShopManagerDaoImpl;
import org.lq.shopping.entity.Menu;
import org.lq.shopping.entity.ShopManager;
import org.lq.shopping.service.ShopManagerService;
import org.lq.shopping.util.RedisUtil;

import com.google.gson.Gson;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class ShopManagerServiceImpl implements ShopManagerService {
	private ShopManagerDao managerDao = new ShopManagerDaoImpl();
	private Jedis jedis=RedisUtil.getJedis();//redis 
	private Gson gson=new Gson();
	private List<ShopManager> managerList = null;
	
	private ShopManagerServiceImpl() {
		init();
	}
	@Override
	public ShopManager findByUname(String uname) {
		jedis.set("shopManager", gson.toJson(managerDao.findByUname(uname)));
		return managerDao.findByUname(uname);
	}
	
	@Override 
	public List<ShopManager> findAllUser() {
		return managerList;
	}
	/**
	 * 待完善
	 */
	@Override
	public List<ShopManager> findPage(int pageIndex, int pageSize) {
			
//		ShopManager shopmanger = new ShopManager();
		Gson gson = new Gson();
		List<ShopManager> list = managerDao.findAll();
		for (ShopManager shopManager : list) {
			String s= gson.toJson(shopManager);
			jedis.lpush("shop_manager", s);
		}
		List<String> stringlist = jedis.lrange("shop_manager", (pageIndex-1)*pageSize,pageIndex*pageSize);
		String rs = jedis.get("shop_manager");
		
		List<ShopManager> managerList = new ArrayList<>();
		for (String str : stringlist) {
			ShopManager sm = gson.fromJson(str, ShopManager.class);
			managerList.add(sm);
		}
		return managerList;
	}

	@Override
	public ShopManager findById(int id) {
		return managerDao.findById(id);
	}

	@Override
	public ShopManager Login(String account, String upass) {
		return managerDao.Login(account, upass);
	}

	@Override
	public boolean reUserPass(int userId) {
		//TODO 重置用户密码 未实现
		return false;
	}

	@Override
	public boolean managerSave(ShopManager sm) {
		String json = gson.toJson(sm);//把管理员对象转换为json
		jedis.set(sm.getMname(), json);//向Redis中添加管理员
		return managerDao.add(sm)>0;
	}

	@Override
	public boolean managerUpdate(ShopManager sm) {
		String json = gson.toJson(sm);//把管理员对象转换为json
		jedis.set(sm.getMname(), json);//向Redis中更新管理员
		return managerDao.update(sm)>0;
	}

	@Override
	public boolean managerDelete(int id) {
		String json = gson.toJson(id);
		jedis.del(json);//向Redis中删除管理员
		return managerDao.delete(id)>0;
	}
	
	@Override
	public boolean disableUserById(int userId) {
		return managerDao.disableUserById(userId)>0;
	}
	/**
	 *  初始化函数
	 */
	private void init() {
		jedis.flushAll();//清空REDIS 库中的所有内容
		managerList=managerDao.findAll();//从数据库中查找全部管理员
		  String json=	null;
	  for (ShopManager managerList : managerList) {
		  json=gson.toJson(managerList);//将管理员转换为json
		  jedis.set(managerList.getMname(), json);//将管理员全部插入到Redis数据库中
	}
	}
	

}
